package com.yanqu.road.server.cmd.activity.springdinner;

import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.springdinner.SpringDinnerActivity;
import com.yanqu.road.server.manager.springdinner.SpringDinnerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C2_SPRINGDINNER_ACTIVITY_ROB_MESSAGE, desc = "抢红包留言")
public class CrossSpringDinnerRobMessageCmd extends CrossCommand {

    private int activityId;
    private long userId;
    private int no;
    private String message;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SpringDinnerProto.CrossRobRedPacketMessageReqMsg reqMsg = SpringDinnerProto.CrossRobRedPacketMessageReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        no = reqMsg.getNo();
        message = reqMsg.getMessage();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SpringDinnerActivity activity = SpringDinnerMgr.getSpringDinnerActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        activity.robTableMessage(serverId,no,userId,message);
    }

    @Override
    public void sendData() {
        if(ret != 0){
            SpringDinnerProto.RobAddMessageRespMsg.Builder respMsg = SpringDinnerProto.RobAddMessageRespMsg.newBuilder();
            respMsg.setRet(ret);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_SPRINGDINNER_RP_ROB_MESSAGE, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

}
